#include "threadwindow.h"
#include "ui_threadwindow.h"

ThreadWindow::ThreadWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ThreadWindow)
{
    ui->setupUi(this);
#if 0
    work = new WorkThread(this);
    connect(work,&WorkThread::sendTotalSize,this,&ThreadWindow::recTotalSize);
    connect(work,&WorkThread::sendLine,this,&ThreadWindow::recData);
    connect(work,&WorkThread::finished,work,&WorkThread::deleteLater);

    sModel = new QStandardItemModel(this);
    ui->tableView->setModel(sModel);
    sModel->setHorizontalHeaderLabels(QStringList()<<"姓名"<<"年龄"<<"分数");
#endif

#if 0//最灵活的，一个任务对应一个线程
    objWorker = new ObjectWorkerThread;
    wThread = new QThread(this);
    objWorker->moveToThread(wThread);
    connect(wThread,&QThread::started,objWorker,&ObjectWorkerThread::doWork1);//这是"启动"处理任务的"信号"绑定逻辑
    connect(objWorker,&ObjectWorkerThread::sendVal,this,&ThreadWindow::recTotalSize);
    connect(wThread,&QThread::finished,objWorker,&ObjectWorkerThread::deleteLater);
#endif

	//线程池
    QThreadPool* phool = QThreadPool::globalInstance();
    phool->setMaxThreadCount(3);
    for(int i=0;i<10;i++)
    {
        wPhool = new WorkerPhool;
        phool->start(wPhool);//主函数里面:任务的“启动信号”由线程池负责，开发者只需提交任务即可
    }
}

ThreadWindow::~ThreadWindow()
{
    delete ui;
}

void ThreadWindow::on_startButton_clicked()
{
//    work->start();
    wThread->start();
}

void ThreadWindow::recTotalSize(int val)
{
//    ui->progressBar->setMaximum(val);
    ui->progressBar->setValue(val);
}

void ThreadWindow::recData(QStringList sl, int lineSize)
{
    static int row =0;
    for(int i=0;i<3;i++)
    {
        sModel->setItem(row,i,new QStandardItem(sl.at(i)));
    }
    row++;

    ui->progressBar->setValue(lineSize);
}
